package com.foreknow.demo8;

/**
 * 主人类
 */
public class Master {

    public void cure(Pet pet) { // pet = Penguin对象
        if (pet.getHealth() < 60) {
            pet.toHospital();
        }
    }

    public void feed(Pet pet) {
        pet.eat();
    }

    public void play(Pet pet) {
        if (pet instanceof Dog){
            Dog dog = (Dog) pet; //如果要调用子类中的新方法，需要强制转换  注意：一定要有继承关系才能转换
            dog.game();
        }else if (pet instanceof Penguin) {
            Penguin penguin = (Penguin) pet;
            penguin.swimming();
        }
    }

    public static void main(String[] args) {
        Master master = new Master();
        Penguin p = new Penguin();
        master.cure(p);
        master.feed(p);
        master.play(p);
        Dog d = new Dog();
        master.feed(d);
        master.play(d);

    }
}
